package backstage.app.service.impl.util;

import java.time.LocalDateTime;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import backstage.app.entity.AdminSys;
import backstage.app.mapper.AdminSysMapper;
import backstage.app.respont.RespontDateJson;
import backstage.app.util.JwtTokenUtil;
import io.jsonwebtoken.Claims;
 

@Component
public class AdminSysUtils {

	@Autowired
	private AdminSysMapper adminSysMapper;
	@Autowired
	private JwtTokenUtil jwtTokenUtil;
	
	public RespontDateJson<?>login(String userName,String passWord){
		if(StringUtils.isEmpty(userName)){
            return RespontDateJson.fuall(userName,"用户不能为空");
        }
        if( StringUtils.isEmpty(passWord)){
            return RespontDateJson.fuall(passWord,"密码不能为空");
        } 
		try {
			AdminSys adminSys=adminSysMapper.selectBylogin(userName, passWord);
			if(adminSys!=null) {
				//存进jwt
				 String token = jwtTokenUtil.getToken(adminSys.getAid()+"") ;
				 return  RespontDateJson.ok(token, "登录成功！");
			}
			return RespontDateJson.fuall(null, "账号或者密码错误登录失败");
		} catch (Exception e) {
			return RespontDateJson.fuall(null, "你在登录时出现了一个问题，服务器异常，请联系开发人员修复");
		}
	
	}
	
	
	
	public RespontDateJson<?>inser(String userName,String passWord){
		if(StringUtils.isEmpty(userName)){
            return RespontDateJson.fuall(userName,"用户不能为空");
        }
        if( StringUtils.isEmpty(passWord)){
            return RespontDateJson.fuall(passWord,"密码不能为空");
        } 
		try {
			AdminSys admin_sys=adminSysMapper.selectByUserNameHave(userName);
			if(admin_sys!=null) {
				return RespontDateJson.fuall(false, "该账号已经存在");
			}
			AdminSys adminSys=new AdminSys();
			adminSys.setUserName(userName);
			adminSys.setPassWord(passWord);
			adminSys.setStauts(1);
			adminSys.setWidthRule(1);
			adminSys.setCreateTime(LocalDateTime.now());
			int index=adminSysMapper.insert(adminSys);
			return index>0?RespontDateJson.ok(adminSys, "添加成功！"):RespontDateJson.fuall(adminSys, "添加失败");
		} catch (Exception e) {
			return RespontDateJson.fuall(null, "添加时出现了一个问题，服务器异常，请联系开发人员修复");
		}
		
	}
	
	public RespontDateJson<?>update(Integer aid,String userName,String passWord){
		if(StringUtils.isEmpty(userName)){
            return RespontDateJson.fuall(userName,"用户不能为空");
        }
        if( StringUtils.isEmpty(passWord)){
            return RespontDateJson.fuall(passWord,"密码不能为空");
        } 
		try {
			AdminSys adminSys=new AdminSys();
			adminSys.setUserName(userName);
			adminSys.setPassWord(passWord);
			adminSys.setAid(aid);
			int index=adminSysMapper.updateByPrimaryKey(adminSys);
			return index>0?RespontDateJson.ok(adminSys, "添加成功！"):RespontDateJson.fuall(adminSys, "添加失败");
		} catch (Exception e) {
			return RespontDateJson.fuall(null, "添加时出现了一个问题，服务器异常，请联系开发人员修复");
		}
		
	}
	
	
	private boolean checkStauts(String  token) {
		try {
			Claims claims = jwtTokenUtil.getTokenClaim(token);
			AdminSys adminSy =adminSysMapper.selectByPrimaryKey(Integer.parseInt(claims.getSubject()));
			if(adminSy!=null) {
				if(adminSy.getStauts()>0) {
					if(adminSy.getWidthRule()>=5) {
						return true;
					}
				}
			}
		}catch (Exception e) {
			 
		}
		 
		return false;
		
	}
	
	
	public RespontDateJson<?>updateStauts(Integer aid,String  token){
		 
		try {
			if(checkStauts(token)==false) {
				return RespontDateJson.fuall(false, "权限不足！");
			}
			AdminSys adminSysaid=adminSysMapper.selectByPrimaryKey(aid);
			if(adminSysaid!=null) {
				int result=-1;
				if(adminSysaid.getStauts()>0) {
					adminSysaid.setStauts(0);
					result=adminSysMapper.updateByPrimaryKey(adminSysaid);
					return  RespontDateJson.ok(result, "已经关闭该用户");
				}else {
					adminSysaid.setStauts(1);
					result=adminSysMapper.updateByPrimaryKey(adminSysaid);
					return  RespontDateJson.ok(result, "已经启用该用户");
				}
			} 
		} catch (Exception e) {
			return RespontDateJson.fuall(null, "添加时出现了一个问题，服务器异常，请联系开发人员修复");
		}
		return null;
		
	}
	
	public RespontDateJson<?>delete(
			 Integer aid,
			 String mid
			 ){
		try {
			if(checkStauts(mid)==false) {
				return RespontDateJson.fuall(false, "权限不足！");
			}
			
			int result=adminSysMapper.deleteByPrimaryKey(aid);
			return result>0?RespontDateJson.ok(result, "删除成功"):RespontDateJson.ok(result, "删除失败");
		} catch (Exception e) {
			// TODO: handle exception
			return RespontDateJson.ok("", "数据库服务器异常");
		}
		 
	}
	
	public RespontDateJson<?>list(Integer page,Integer limit){
		IPage<AdminSys>ipage=new Page<AdminSys>(page,limit);
		
		List<AdminSys>listadmin=adminSysMapper.selectPage(ipage, null).getRecords();
		ArrayList<AdminSys>array=new ArrayList<AdminSys>(11);
		Iterator<AdminSys>it=listadmin.iterator();
		while (it.hasNext()) {
			AdminSys adminSys = (AdminSys) it.next();
			adminSys.setPassWord(adminSys.getPassWord().substring(0, 2)+"*********");
			adminSys.setUserName(adminSys.getUserName().substring(0, 2)+"*********");
			array.add(adminSys);
		}
		return  RespontDateJson.ok(array,adminSysMapper.selectCount(null), "成功");
	}
}
